package com.service;


import com.mapper.DormitoryMapper;
import com.pojo.Dormitory;
import com.utils.MyBatisSqlSessionFactory;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;

/**
 * @ClassName DormitoryServiceImpl
 * @Description TODO
 * @Author Kx
 * @Date 2025/4/21 19:54
 * @Version 1.0
 */
public class DormitoryService {
    /***
     * @author: kx
     * @date: 2025/4/22 10:50
     * @return java.util.List<com.pojo.Dormitory>
     * @description: 查询所有宿舍信息
    */
    public List<Dormitory> findAllDormitory() {

        List<Dormitory> dormitoryList = null;

        // 获取sqlSession对象
        SqlSession sqlSession = MyBatisSqlSessionFactory.openSession();

        // 获取mapper接口对象
        DormitoryMapper mapper = sqlSession.getMapper(DormitoryMapper.class);

        // 执行查询语句
        dormitoryList = mapper.findAllDormitory();

        return dormitoryList;
    }

    /***
     * @author: kx
     * @date: 2025/4/22 10:51
     * @param dormitory : 宿舍信息
     * @description: 添加宿舍信息
    */
    public void insertDormitory(Dormitory dormitory) {
        // 获取sqlSession对象
        SqlSession sqlSession = MyBatisSqlSessionFactory.openSession();

        try {
            // 获取mapper接口对象
            DormitoryMapper mapper = sqlSession.getMapper(DormitoryMapper.class);

            // 执行添加语句
            mapper.insertDormitory(dormitory);

            // 提交事务
            sqlSession.commit();
        } catch (RuntimeException e) {
            // 事物回滚
            sqlSession.rollback();
        } finally {
            // 关闭sqlSession对象
            sqlSession.close();
        }
    }

    /***
     * @author: kx
     * @date: 2025/4/23 11:34
     * @param map :宿舍信息
     * @description: 使用map添加宿舍信息
    */
    public void insertDormitory(Map<String,Object> map) {
        // 获取sqlSession对象
        SqlSession sqlSession = MyBatisSqlSessionFactory.openSession();

        try {
            // 获取mapper接口对象
            DormitoryMapper mapper = sqlSession.getMapper(DormitoryMapper.class);

            // 执行添加语句
            mapper.insertDormitory2(map);

            // 提交事务
            sqlSession.commit();
        } catch (RuntimeException e) {
            // 事物回滚
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            // 关闭sqlSession对象
            sqlSession.close();
        }
    }

    /***
     * @author: kx
     * @date: 2025/4/23 11:35
     * @param dormitoryId : 宿舍id
     * @description: 删除宿舍信息
    */
    public void deleteDormitory(String dormitoryId) {
        // 获取sqlSession对象
        SqlSession sqlSession = MyBatisSqlSessionFactory.openSession();

        try {
            // 获取mapper接口对象
            DormitoryMapper mapper = sqlSession.getMapper(DormitoryMapper.class);

            // 执行添加语句
            mapper.deleteDormitory(dormitoryId);

            // 提交事务
            sqlSession.commit();
        } catch (RuntimeException e) {
            // 事物回滚
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            // 关闭sqlSession对象
            sqlSession.close();
        }
    }

    /***
     * @author: kx
     * @date: 2025/4/23 11:41
     * @param dormitory : 需要修改的宿舍信息
     * @description: 修改宿舍信息
    */
    public void updateDormitory(Dormitory dormitory) {
        // 获取sqlSession对象
        SqlSession sqlSession = MyBatisSqlSessionFactory.openSession();

        try {
            // 获取mapper接口对象
            DormitoryMapper mapper = sqlSession.getMapper(DormitoryMapper.class);

            // 执行添加语句
            mapper.updateDormitory(dormitory);

            // 提交事务
            sqlSession.commit();
        } catch (RuntimeException e) {
            // 事物回滚
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            // 关闭sqlSession对象
            sqlSession.close();
        }
    }
}
